package com.sgb.goods.view.activity;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityBrandManagerBinding;
import com.sgb.goods.entity.BrandManagerEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.utils.helper.SmartRefreshLayoutHelper;
import com.sgb.goods.viewmodel.BrandManagerModel;
import com.swgk.core.dialog.RoundCornerDialog;

import java.util.Objects;

/**
 * 品牌管理
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_BRAND_MANAGER)
public class BrandManagerActivity extends BaseActivity<GoodsActivityBrandManagerBinding, BrandManagerModel>
        implements SmartRefreshLayoutHelper {

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_brand_manager;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(BrandManagerModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText("品牌管理");
        mViewModel.getBrandList(false);
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mDataBinding.tvAdd.setOnClickListener(v -> {
            // 添加品牌
            ARouter.getInstance().build(Constants.ARouter.AR_ADD_BRAND).navigation();
        });
        mDataBinding.rlvLayout.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getBrandList(false);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getBrandList(true);
            }
        });
        mViewModel.adapter.setOnItemChildClickListener((adapter, view, position) -> {
            if(R.id.tv_edit == view.getId()) {
                //if(mViewModel.getBrandOperationPermission()) {
                    BrandManagerEntity entity = ((BrandManagerEntity) Objects.requireNonNull(adapter.getItem(position)));
                    // 编辑品牌
                    ARouter.getInstance().build(Constants.ARouter.AR_ADD_BRAND)
                            .withString("id", entity.id)
                            .withString("name", entity.name)
                            .withString("content", entity.content)
                            .navigation();
                //}
            } else if(R.id.tv_delete == view.getId()) {
                //if(mViewModel.getBrandOperationPermission()) {
                    showDialog(((BrandManagerEntity) Objects.requireNonNull(adapter.getItem(position))).id, position);
                //}
            }
        });
    }

    @Override
    protected void initObserve() {
        mViewModel.brandList.observe(this, orderList -> {
            // SmartRefreshLayout 代理UI处理
            refreshUi(mDataBinding.rlvLayout.refreshLayout, mViewModel.adapter, orderList);
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mViewModel.getBrandList(false);
    }

    /**
     * 删除的dialog
     */
    private void showDialog(String id, int position) {
        View view = View.inflate(BrandManagerActivity.this, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(BrandManagerActivity.this, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.setCanceledOnTouchOutside(false);
        roundCornerDialog.show();
        ((TextView) view.findViewById(R.id.tv_message)).setText("确定要删除吗?");
        view.findViewById(R.id.tv_logout_confirm).setOnClickListener(v -> {
            roundCornerDialog.dismiss();
            mViewModel.delete(id, position);
        });
        view.findViewById(R.id.tv_logout_cancel).setOnClickListener(v -> roundCornerDialog.dismiss());
    }
}
